#ifndef __MY_UTIL__
#define __MY_UTIL__
#include <iostream>
#include <fstream>
#include <sstream>
#include <memory>
#include <string>
#include <unistd.h>
#include <sys/stat.h>
#include <jsoncpp/json/json.h>
using namespace std;
namespace aod
{
    class FileUtil
    {
    private:
        string _filename; // 文件名称
    public:
        FileUtil(const string filename) : _filename(filename) {};
        bool Exists() // 判断文件是否存在
        {
            int ret = access(_filename.c_str(), F_OK);
            if (ret != 0)
            {
                cout << "fiel no exists" << endl;
                return false;
            }
            return true;
        }
        size_t Size() // 获取文件大小
        {
            if (this->Exists() == false)
            {
                return 0;
            }
            struct stat st;
            int ret = stat(_filename.c_str(), &st);
            if (ret != 0)
            {
                cout << "get file stat failed" << endl;
                return 0;
            }
            return st.st_size;
        }
        bool GetContent(string &body) // 读取文件数据到body
        {
            ifstream ifs;
            ifs.open(_filename, ios::binary);
            if (ifs.is_open() == false)
            {
                cout << "open file failed" << endl;
                return false;
            }
            int lens = this->Size();
            body.resize(lens);
            ifs.read(&body[0], lens);
            if (ifs.good() == false)
            {
                cout << "read file content failed" << endl;
                ifs.close();
                return false;
            }
            ifs.close();
            return true;
        }
        bool SetContent(const string &body) // 向文件中写入数据
        {
            ofstream ofs;
            ofs.open(_filename, ios::binary);
            if (ofs.is_open() == false)
            {
                cout << "open file failed" << endl;
                return false;
            }
            ofs.write(body.c_str(), body.size());
            if (ofs.good() == false)
            {
                cout << "write file content failed" << endl;
                ofs.close();
                return false;
            }
            ofs.close();
            return true;
        }
        bool CreateDirectory() // 针对文件创建目录;
        {
            if (this->Exists())
            {
                return true;
            }
            mkdir(_filename.c_str(), 0777);
            return true;
        }
    };

    class JsonUtil
    {
    public:
        static bool Serialize(const Json::Value &value, string *body)//序列化
        {
            Json::StreamWriterBuilder swb;
            unique_ptr<Json::StreamWriter> sw(swb.newStreamWriter());
            ostringstream os;
            int ret=sw->write(value, &os);
            if(ret!=0)
            {
                cout<<"write failed"<<endl;
            }
            string ss = os.str();
            *body=ss;
            return true;
        }
        static bool UnSerialize(const string &body, Json::Value *value)
        {
            Json::CharReaderBuilder crb;
            unique_ptr<Json::CharReader> cr(crb.newCharReader());
            string terr;
            bool ret =cr->parse(body.c_str(),body.c_str()+body.size(),value,&terr);
            if(ret==false)
            {
                cout<<"UnSerialize failed"<<endl;
                return false;
            }
            return true;
        }
    };
    
}
#endif
